package steak.xteamy.VoiceChat;

import de.maxhenkel.voicechat.api.Group;
import de.maxhenkel.voicechat.api.VoicechatConnection;
import de.maxhenkel.voicechat.api.VoicechatServerApi;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class GroupManager {

    public static Group WOLF_GROUP;
    public static Group DEATH_GROUP;

    public static List<UUID> spectatorPlayers = new ArrayList<>();

    private static final GroupManager INSTANCE = new GroupManager();
    public static GroupManager getInstance() {
        return INSTANCE;
    }

    public void initialize(VoicechatServerApi API) {
        WOLF_GROUP = createGroup(API, "WOLF");
        DEATH_GROUP = createGroup(API, "DEATH");

        Plugin.groups.put(WOLF_GROUP.getName(), WOLF_GROUP);
        Plugin.groups.put(DEATH_GROUP.getName(), DEATH_GROUP);
    }

    private Group createGroup(VoicechatServerApi API, String name) {
        return API.groupBuilder()
                .setName(name)
                .setId(UUID.randomUUID())
                .setPassword(name)
                .setPersistent(true)
                .setType(Group.Type.NORMAL)
                .setHidden(true)
                .build();
    }

    public static void update(UUID uuid, Group group) {
        VoicechatServerApi api = Plugin.voicechatServerApi;
        VoicechatConnection connection = api.getConnectionOf(uuid);

        if (connection != null) {
            connection.setGroup(group);

            if (group != null && group.getId().equals(GroupManager.DEATH_GROUP.getId())) {
                GroupManager.spectatorPlayers.add(uuid);
            }
            if (group == null) {
                GroupManager.spectatorPlayers.remove(uuid);
            }
        }
    }
}
